package cn.hfatec.shms.web.xm.xmCgmj.action;

import cn.hfatec.shms.exception.NotAuthException;
import cn.hfatec.shms.system.user.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.web.xm.xmCgmj.model.XmCgmjParams;
import cn.hfatec.shms.web.xm.xmCgmj.model.XmCgmj;
import cn.hfatec.shms.web.xm.xmCgmj.service.XmCgmjService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;



/**
 *
 * @author hfatec codegen
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Controller
@RequestMapping("/xm/xmCgmj")
public class XmCgmjAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "XmCgmjService")
    private XmCgmjService xmCgmjService;


    /**
     *
     * 查询列表
     *
     */
    @RequestMapping("/list")
    public String list(XmCgmjParams xmCgmjParams, Page page, Model model) {
        Table table = xmCgmjService.queryXmCgmjTable(xmCgmjParams, page);
        XmCgmj xmCgmjHj=xmCgmjService.XmCgmjHj(xmCgmjParams);
        model.addAttribute("xmCgmjHj", xmCgmjHj);
        model.addAttribute("xmCgmjsTable", table);
        model.addAttribute("xmCgmjParams", xmCgmjParams);
        return "/xm/xmCgmj/list";
    }

    /**
     *
     * 异步查询列表
     *
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(XmCgmjParams xmCgmjParams, Page page, Model model) {
        try {
            list(xmCgmjParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xm/xmCgmj/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到新增页
     *
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model, HttpServletRequest request) {
        try {
            User user = (User) request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
            if (user == null) {
                throw new NotAuthException("用户未授权");
            }
            XmCgmj xmCgmj = new XmCgmj();
            model.addAttribute("xmCgmj", xmCgmj);
            xmCgmj.setUserName(user.getUserName());
            xmCgmj.setFieldK(user.getOrgId());
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String gjDate = df.format(new Date());
            xmCgmj.setBillTime(gjDate);
            List ListA = xmCgmjService.XmCgmjXmA();
            List ListB = xmCgmjService.XmCgmjXmB();
            List ListC = xmCgmjService.XmCgmjXmC();
            List ListD = xmCgmjService.XmCgmjXmD();
            List ListE = xmCgmjService.XmCgmjXmE();
            model.addAttribute("xmCgmjA", ListA);
            model.addAttribute("xmCgmjB", ListB);
            model.addAttribute("xmCgmjC", ListC);
            model.addAttribute("xmCgmjD", ListD);
            model.addAttribute("xmCgmjE", ListE);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xm/xmCgmj/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到编辑页
     *
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(String id, Model model) {

        try {
            XmCgmj xmCgmj = xmCgmjService.XmCgmjTime(id);
            List ListA = xmCgmjService.XmCgmjXmAedi(id);
            List ListB = xmCgmjService.XmCgmjXmBedi(id);
            List ListC = xmCgmjService.XmCgmjXmCedi(id);
            List ListD = xmCgmjService.XmCgmjXmDedi(id);
            List ListE = xmCgmjService.XmCgmjXmEedi(id);
            model.addAttribute("xmCgmjA", ListA);
            model.addAttribute("xmCgmjB", ListB);
            model.addAttribute("xmCgmjC", ListC);
            model.addAttribute("xmCgmjD", ListD);
            model.addAttribute("xmCgmjE", ListE);
            model.addAttribute("xmCgmj", xmCgmj);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xm/xmCgmj/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 根据IDs删除数据
     *
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteXmCgmj(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if(ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            xmCgmjService.deleteXmCgmjBatch(idList);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     *
     * 保存数据
     *
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveXmCgmj(String[] ids, String[] DTypes, String[] XTypes, String[] fieldAs,String[] fieldBs,String[] fieldCs,String[] fieldDs,String[] fieldEs,@Valid XmCgmj xmCgmj, BindingResult result, HttpServletRequest request) throws NotAuthException {

        try {
            User user = (User) request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
            if (user == null) {
                throw new NotAuthException("用户未授权");
            }
            if(result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            if (xmCgmj.getId()!= null) {
                xmCgmjService.saveXmCgmj(ids, DTypes, XTypes, fieldAs,fieldBs,fieldCs,fieldDs,fieldEs,xmCgmj,user);
            }else{
            XmCgmj xmCgmjhd =   xmCgmjService.XmCgmjTime(xmCgmj.getBillTime());
            if(xmCgmjhd==null)
            xmCgmjService.saveXmCgmj(ids, DTypes, XTypes, fieldAs,fieldBs,fieldCs,fieldDs,fieldEs,xmCgmj,user);
       else  {
                logger.error("该天数据已存在,请直接编辑！");
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE,"该天数据已存在,请直接编辑！").toString();

            }}
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据XmCgmj保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }
}
